/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sample.test.cc.cool;

import BESA.Agent.Event.DataBESA;
import BESA.Exception.ExceptionBESA;
import BESA.SystemLevel.AdmBESA;
import BESA.SystemLevel.Directory.AgHandler;
import BESA.SystemLevel.transactions.agent.TransactionalEventBESA;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import sample.test.cc.simpleagent.ReadGuard;
import sample.test.cc.simpleagent.SimpleDataRead;
import sample.test.cc.simpleagent.SimpleDataWrite;
import sample.test.cc.simpleagent.WriteGuard;
import sample.test.cc.testfile.EventInfo;
import sample.test.cc.testfile.Process;
import sample.test.cc.testfile.TestLoader;

/**
 *
 * @author Andrea
 */
public class TransactionManager_TestFile {

    private static final String IP_ADD = "127.0.0.1";
    private static final int RMI_PORT = 9899;
    private static final String MC_ADD = "230.0.0.1";
    private static final int MC_PORT = 2222;
    private static final int GCC_PORT = 9871;

    public static void main(String[] args) {
        try {
            AdmBESA admLocal = AdmBESA.createInstance("Asma_1", 1.0, IP_ADD, RMI_PORT, MC_ADD, MC_PORT, GCC_PORT);
            Map<Long, Process> transactions = (new TestLoader()).getTransactions();
           
            
            for (int i = 1; i <= transactions.size(); i++) {
                Long key=Long.valueOf(String.valueOf(i));
                if(transactions.get(key).isOpen()){
                    openTransaction(admLocal);
                }else if(transactions.get(key).isClose()){
                    closeTransaction(admLocal, transactions.get(key).getIdTransactionClose());
                }else{
                    execEvent(admLocal, transactions.get(key).getEvent());
                }

            }



        } catch (Exception ex) {
            Logger.getLogger(TransactionManager_TestFile.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private static void execEvent(AdmBESA admLocal, EventInfo evInfo) {
        try {
            System.out.println("EJECUTAR EVENTO :"+evInfo.toString());
            AgHandler ah = admLocal.getHandlerByAlias(evInfo.aliasAgResource);
            DataBESA sdr = null;
            String evType = null;
            if (evInfo.getIntention().toString().equals("READ")) {
                evType = ReadGuard.class.getName();
                sdr = new SimpleDataRead();
            } else if (evInfo.getIntention().toString().equals("WRITE")) {
                evType = WriteGuard.class.getName();
                sdr = new SimpleDataWrite(evInfo.newValue);
            }
            TransactionalEventBESA ev = new TransactionalEventBESA(evType, sdr, evInfo.idTransaction);
            ah.sendEvent(ev);

        } catch (ExceptionBESA ex) {
            ex.printStackTrace();
        }
    }

    private static void openTransaction(AdmBESA admLocal) {
        try {
            long idTransaction = admLocal.openTransaction();
            System.out.println("---------OPEN TRANSACTION ID:" + idTransaction);
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            Logger.getLogger(TransactionManager_TestFile.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExceptionBESA ex) {
            Logger.getLogger(TransactionManager_TestFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void closeTransaction(AdmBESA admLocal, Long id) {
        try {
            Thread.sleep(5000);
            System.out.println("---------CLOSE TRANSACTION ID:" + id);
            admLocal.closeTransaction(id);
        } catch (InterruptedException ex) {
            Logger.getLogger(TransactionManager_TestFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
